//
//  CatogryAllSpecialDetailViewController.m
//  project
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CatogryAllSpecialDetailViewController.h"
#import "CatogryAllSpecialDetailCell.h"
#import "PKRequestManager.h"
#import "StrategyDetailsViewController.h"
#import "OtherTableViewModel.h"
#import "FMDatabase.h"

@interface CatogryAllSpecialDetailViewController () <UITableViewDataSource,UITableViewDelegate>

//创建一个tableView
@property(nonatomic,strong)UITableView *specialDetailTableView;
//创建一个接收数据的数组
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
//创建数据库属性
@property(nonatomic,strong)FMDatabase *dataBase;
//创建一个数组，接收数据库保存的所有id
@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation CatogryAllSpecialDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //添加title
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 10, kWidth - 160, 20)];
    titleLabel.text = _titleLabel;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    _specialDetailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _specialDetailTableView.backgroundColor = [UIColor whiteColor];
    _specialDetailTableView.delegate = self;
    _specialDetailTableView.dataSource = self;
    [_specialDetailTableView registerClass:[CatogryAllSpecialDetailCell class] forCellReuseIdentifier:@"CatogryAllSpecialDetailCell"];
    _specialDetailTableView.rowHeight = 210;
    _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    [self.view addSubview:_specialDetailTableView];
    
    [self requestData];
    
    //打开数据库，查看id是否保存，保存的id点赞的心变红
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(9, 1, 1)firstObject]stringByAppendingPathComponent:@"project.db"];
    _dataBase = [FMDatabase databaseWithPath:filepath];
    [_dataBase open];
    
    _array = [NSMutableArray array];
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from AllLove"];
    while ([resultSet next])
    {
        NSString *loveID = [resultSet stringForColumn:@"loveid"];
        if (loveID == nil)
        {
            
        }
        else
        {
            [self.array addObject:loveID];
        }
    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CatogryAllSpecialDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CatogryAllSpecialDetailCell" forIndexPath:indexPath];
    cell.model = _dataSourceArray[indexPath.row];
    for (NSString *str in self.array)
    {
        if ([cell.model.Myid isEqualToString:str])
        {
            [cell.loveButton1 setImage:[UIImage imageNamed:@"iconfont-icon503-2"] forState:(UIControlStateNormal)];
        }
    }
    return cell;
}

-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionaryWithCapacity:0];
    
    [PKRequestManager requestWithTypeP:GET urlString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?gender=1&generation=1&limit=20&offset=0",_id] parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dict = dic[@"data"];
        NSArray *arr = dict[@"posts"];
        for (NSDictionary *dicModel in arr)
        {
            OtherTableViewModel *model = [[OtherTableViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dicModel];
            [_dataSourceArray addObject:model];
        }
        [_specialDetailTableView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailsViewController *detailVC = [[StrategyDetailsViewController alloc]init];
    OtherTableViewModel *model = _dataSourceArray[indexPath.row];
    detailVC.contentId = model.Myid;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
